package com.wing.product.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wing.common.utils.PageResult;
import com.wing.product.dao.*;
import com.wing.product.model.entity.*;
import com.wing.product.model.request.WarehouseProductForm;
import com.wing.product.model.response.WarehouseMaterialRequisitionRecordVO;
import com.wing.product.model.response.WarehouseProductInventoryVO;
import com.wing.product.model.response.WarehouseProductVO;
import com.wing.web.utils.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.*;
import java.util.stream.Collectors;

@Service
public class WarehouseProductServiceImpl extends ServiceImpl<WarehouseProductDao, WarehouseProduct> implements WarehouseProductService {

    @Autowired
    WarehouseOrderService warehouseOrderService;
    @Autowired
    WarehouseOrderDetailDao warehouseOrderDetailDao;
    @Autowired
    WarehouseShipmentOrderService warehouseShipmentOrderService;

    @Autowired
    private WarehouseSignOrderService warehouseSignOrderService;
    @Autowired
    WarehouseProductVarietyDao warehouseProductVarietyDao;
    @Autowired
    WarehouseProductMarkDao warehouseProductMarkDao;
    @Autowired
    WarehouseProductInventoryDao warehouseProductInventoryDao;
    @Autowired
    WarehouseProductDao warehouseProductDao;

    @Override
    public Page<WarehouseProductVO> pageList4appTwo(Page page, Map paramMap) {
        List<WarehouseProductVO> list = baseMapper.list4appTwo(page, paramMap);
        List<Long> longList = list.stream().map(WarehouseProductVO::getId).collect(Collectors.toList());
        if (CollectionUtils.isNotEmpty(longList)){
            LambdaQueryWrapper<WarehouseProductInventory> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(WarehouseProductInventory::getIsDeleted,0)
                    .in(WarehouseProductInventory::getWarehouseProductId,longList);
            List<WarehouseProductInventory> warehouseProductInventories = warehouseProductInventoryDao.selectList(lambdaQueryWrapper);
            for (WarehouseProductVO pv : list){
                Integer quantitySum = 0;
                Double netWeightSum = 0.0;
                Double grossWeightSum = 0.0;
                for (WarehouseProductInventory pi : warehouseProductInventories){
                    if (pv.getId().equals(pi.getWarehouseProductId())){
                        if (pi.getQuantity() != null){
                            quantitySum += pi.getQuantity();
                        }
                        if (pi.getGrossWeight() != null){
                            grossWeightSum += pi.getGrossWeight();
                        }
                        if (pi.getNetWeight() != null){
                            netWeightSum += pi.getNetWeight();
                        }
                    }
                }
                pv.setInventoryQuantity(quantitySum);
                pv.setInventoryNetWeight(netWeightSum);
                pv.setInventoryGrossWeight(grossWeightSum);
            }
        }
        page.setRecords(list);
        return page;
    }

    @Override
    public Page<WarehouseProductVO> pageList4app(Page pg, Map<String, Object> paramMap) {
        List<WarehouseProductVO> list = this.baseMapper.list4app(pg, paramMap);
        pg.setRecords(list);
        return pg;
    }

    /**
     * 保存
     *
     * @param warehouseProductForm 仓库产品形式
     * @return {@code Boolean }
     */
    @Override
    public Boolean save(WarehouseProductForm warehouseProductForm) {

        WarehouseProduct warehouseProduct = new WarehouseProduct();
        CopyOptions copyOptions = CopyOptions.create()
                .setEditable(WarehouseProduct.class)
                .setIgnoreError(true)
                .setIgnoreNullValue(true);
        BeanUtil.copyProperties(warehouseProductForm, warehouseProduct,copyOptions);
        //2025年3月25日 18:59:54修改为从下拉框里面选择所属客户，替换原来的从登陆客户中选择
//        warehouseProduct.setCustomerId(JwtUtils.getDepartmentId());
        warehouseProduct.setLength(warehouseProductForm.getProductLength());
        warehouseProduct.setCustomerId(warehouseProductForm.getCustomerNameId());
        baseMapper.insert(warehouseProduct);
        return true;
    }

    @Override
    public Boolean checkProductCode(String productCode) {
        List<WarehouseProduct> warehouseProductList=warehouseProductDao.findBbyProductCode(productCode);
        if(warehouseProductList.size()>0){
            return  false;
        }
        return true;
    }


    @Override
    public Boolean update(WarehouseProductForm warehouseProductForm) {
        WarehouseProduct warehouseProduct = new WarehouseProduct();
        CopyOptions copyOptions = CopyOptions.create()
                .setEditable(WarehouseProduct.class)
                .setIgnoreError(true)
                .setIgnoreNullValue(true);
        BeanUtil.copyProperties(warehouseProductForm, warehouseProduct,copyOptions);
        warehouseProduct.setLength(warehouseProductForm.getProductLength());
        return this.updateById(warehouseProduct);
    }

    @Override
    public List<WarehouseProductVO> selectProduct(String warehouseSignsOrderId){
        List<WarehouseProductVO> warehouseProductVOList = new ArrayList<>();
        warehouseProductVOList = this.baseMapper.selectProduct(warehouseSignsOrderId);

        return warehouseProductVOList;
    }

    @Override
    public List<WarehouseProductInventoryVO> selectProductDetail(Integer status, Long qualityTestingId){
        return warehouseProductInventoryDao.selectListVO(status,qualityTestingId);
    }

    @Override
    public  PageResult<WarehouseProductInventoryVO> selectRecord(String id, String warehouseId,List<String> bundleCodeList,Integer page,Integer limit){

        Page<WarehouseProductInventoryVO> warehouseProductInventoryVOPage = new Page<>(page, limit);
        WarehouseProductVO warehouseProductVO = this.baseMapper.selectProductPrice(id);
        //查询产品下所有明细
        List<WarehouseProductInventoryVO> warehouseProductInventoryVOList = warehouseProductInventoryDao.selectListRecord(warehouseProductInventoryVOPage,warehouseProductVO.getId(),warehouseId,bundleCodeList);
        //库区数量
        Set<Long> warehouseAreaSet = warehouseProductInventoryVOList.stream().map(WarehouseProductInventoryVO::getWarehouseAreaId).collect(Collectors.toSet());
        //库位数量
        Set<Long> warehouseShelfSpaceSet = warehouseProductInventoryVOList.stream().map(WarehouseProductInventoryVO::getWarehouseShelfSpaceId).collect(Collectors.toSet());
        warehouseProductVO.setInventoryQuantity(warehouseProductInventoryVOList.size());
        warehouseProductVO.setWarehouseAreaSum(warehouseAreaSet.size());
        warehouseProductVO.setWarehouseShelfSpaceSum(warehouseShelfSpaceSet.size());
        warehouseProductVO.setWarehouseProductInventoryVOList(warehouseProductInventoryVOList);
        IPage<WarehouseProductInventoryVO> warehouseProductInventoryVOIPage = warehouseProductInventoryVOPage.setRecords(warehouseProductInventoryVOList);
//        warehouseProductInventoryVOIPage.setRecords(warehouseProductInventoryVOList);
//        warehouseProductInventoryVOIPage.setTotal(Long.valueOf(warehouseProductInventoryVOList.size()));
        return  new PageResult<>(warehouseProductInventoryVOIPage.getRecords(), warehouseProductInventoryVOIPage.getTotal());
    }

    @Override
    public  PageResult<WarehouseProductInventoryVO> selectProductDetailTwo(String warehouseProductId,String warehouseId,
                                                                     String warehouseAreaId,String warehouseShelfSpaceId,
                                                                     String userBundleCode,String firstEntryDate,Integer page,Integer limit
){
        Page<WarehouseProductInventoryVO> warehouseProductInventoryVOPage = new Page<>(page, limit);
        List<WarehouseProductInventoryVO> warehouseProductInventoryVOList = warehouseProductInventoryDao.selectListVOTwo(warehouseProductInventoryVOPage, warehouseProductId, warehouseId, warehouseAreaId, warehouseShelfSpaceId, userBundleCode, firstEntryDate);
        IPage<WarehouseProductInventoryVO> warehouseProductInventoryVOIPage = warehouseProductInventoryVOPage.setRecords(warehouseProductInventoryVOList);
        return new PageResult<>(warehouseProductInventoryVOIPage.getRecords(), warehouseProductInventoryVOIPage.getTotal());
    }
}
